package com.mengtian_tech.tju.dataquery.api.service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 人员进出记申请服务类
 */
@Service
public class PersonVisitApplyService {
	
	@Autowired
	@Qualifier("jdbcTemplateDataAi")
	JdbcTemplate jdbcTemplateDataAi;
	

	@Autowired
	@Qualifier("jdbcTemplatePersonVisitApply")
	JdbcTemplate jdbcTemplatePersonVisitApply;
	
	/**
	 * 同步人员入校的申请数据，每次获取1000条进行同步
	 */
	public synchronized int aysncPersonVisitApply() {
		//查询条件
		String where = "";
		
		//首先获取最近的同步信息
		List<Map<String,Object>> asyncList = this.jdbcTemplateDataAi.queryForList("select * from async_log_visit_apply where sync_count > 0 order by id desc limit 0,1");
		if(asyncList.size() > 0) {
			Map<String,Object> map = asyncList.get(0);
			if(map != null && map.get("end_id") != null) {
				where = " where id >  " + map.get("end_id") + " ";
			}
		}
		
		
		Date start = new Date();
		int count = 0;
		BigInteger startId = null;
		BigInteger endId = null;
		Date lastCreateTime = null;
		
		List<Map<String,Object>> list =  this.jdbcTemplatePersonVisitApply.queryForList("select * from  fymobile.epa_apply_detail  "+ where +" order by id asc LIMIT 0, 1000");
		count = list.size();
		
		try {		
			for(Map<String,Object> item:list) {
				if(startId == null) startId = new BigInteger(item.get("id").toString());
				endId =  new BigInteger(item.get("id").toString());
				lastCreateTime = (Date) item.get("submit_time");
								
				insert(item);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			Date end = new Date();
			long seconds = (end.getTime() - start.getTime())/1000;
			
			//插入数据库记录
			this.jdbcTemplateDataAi.update(
					"insert into async_log_visit_apply(task_begin,task_end,sync_count,start_id,end_id,last_create_time,duration) values(?,?,?,?,?,?,?)",
					start,end,count,startId,endId,lastCreateTime,seconds);
			
			
		}
		return count;
		
	}

	private void insert(Map<String, Object> item) {
		try {
			
			this.jdbcTemplateDataAi.update("insert into visit_apply(role,name,id_number,dept_id,dept_name,major,card_no,"
					+ " class_name,contact,submit_time,approval_status,reason,remark,approval_time,"
					+ " visitor_name,sex,relation,visitor_card_no,visit_time,visit_start_time,"
					+ " visit_end_time,visitor_dept,visitor_job,visitor_phone,pass_type,car_num,visit_place,health,"
					+ " promise,visitor_address,apply_id) "
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
					item.get("role"),item.get("name"),item.get("id_number"),item.get("dept_id"),item.get("dept_name"),item.get("major"),item.get("card_no"),
					item.get("class_name"),item.get("contact"),item.get("submit_time"),item.get("approval_status"),item.get("reason"),item.get("remark"),item.get("approval_time"),
					item.get("visitor_name"),item.get("sex"),item.get("relation"),item.get("visitor_card_no"),item.get("visit_time"),item.get("visit_start_time"),
					item.get("visit_end_time"),item.get("visitor_dept"),item.get("visitor_job"),item.get("visitor_phone"),item.get("pass_type"),item.get("car_num"),item.get("visit_place"),item.get("health"),
					item.get("promise"),item.get("visitor_address"),item.get("id"));
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
